//go:build !386 && !amd64
// +build !386,!amd64

package init

import (
	"fmt"
	"github.com/Dliv3/Venom/node"
	"time"

	"github.com/Dliv3/Venom/agent/cli"
	"github.com/Dliv3/Venom/agent/dispather"
	"github.com/Dliv3/Venom/netio"
)

func InitNode() {
	if cli.Args.Mode == cli.LISTEN_MODE {
		// 监听端口
		netio.InitNode(
			"listen",
			fmt.Sprintf("0.0.0.0:%d", uint16(cli.Args.LocalPort)),
			dispather.AgentServer, false, 0)
	} else {
		// 无限重连，时间间隔5s
		go func() {
			for {
				if node.CurrentNode.CommandHandlerCount == 0 {
					// 连接端口
					netio.InitNode(
						"connect",
						fmt.Sprintf("%s:%d", cli.Args.RemoteIP, uint16(cli.Args.RemotePort)),
						dispather.AgentClient, false, 0)
				}

				// 设置无需重连，则退出循环
				if cli.Args.NoReconnect == true {
					break
				}

				time.Sleep(5 * time.Second)
			}
		}()
	}

	select {}
}
